package com.xunsiya.controller;

import com.xunsiya.bean.ResultData;
import com.xunsiya.common.config.util.ConstantInfo;
import com.xunsiya.module.device.vo.VideoStreamDeviceDetail;
import com.xunsiya.service.module.device.VideoStreamCompanyService;
import com.xunsiya.tools.common.msg.Msg;
import com.xunsiya.tools.common.string.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 设备管理Controller
 *
 * @author xml
 * @version 1.0
 *          <p>
 *          <p>修订人		修订时间			描述信息
 *          <p>-----------------------------------------------------
 *          <p>xml		2018/11/6		初始创建
 */
@RestController
public class DeviceController {

    private static Logger log = Logger.getLogger(DeviceController.class);

    @Autowired
    private VideoStreamCompanyService videoStreamCompanyService;

    /**
     * 根据设备ID获取视频传输相关参数配置
     *
     * @param zwanNumber 紫万账号
     * @return
     */
    @PostMapping("/videostream/{zwanNumber}")
    public ResultData getDeviceConfig(@PathVariable String zwanNumber) {
        ResultData msg = new ResultData(true);

        //得到返回的结果
        Map<String, Object> map = videoStreamCompanyService.queryByZwan(zwanNumber);
        if (!CollectionUtils.isEmpty(map)) {
            //设置结果数据到data中
            msg.setData(map);
            //结果消息
            msg.setMsg(ConstantInfo.HttpCode.HTTP_200.getDesc());
            log.info(ConstantInfo.HttpCode.HTTP_200.getDesc() + ",执行成功！");
        } else {
            msg.setCode(ConstantInfo.HttpCode.HTTP_400.getValue());
            msg.setMsg(ConstantInfo.HttpCode.HTTP_400.getDesc());
            log.info(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",执行失败！");
        }
        return msg;
    }

    /**
     * 保存视频传输通话记录
     *
     * @param videoStreamDeviceDetail 设备通话记录
     * @return
     */
    @PostMapping("/videostream/saveRecord")
    public ResultData saveRecord(@RequestBody VideoStreamDeviceDetail videoStreamDeviceDetail) {
        ResultData msg = new ResultData(false);
        //判断紫万账号是否为空
        if (StringUtil.isEmpty(videoStreamDeviceDetail.getSource())) {
            msg.setCode(ConstantInfo.HttpCode.HTTP_400.getValue());
            msg.setMsg(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",紫万账号不能为空！");
            log.info(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",紫万账号不能为空！");
            return msg;
        }
        //判断参与者是否为空
        if (StringUtil.isEmpty(videoStreamDeviceDetail.getParticipant())) {
            msg.setCode(ConstantInfo.HttpCode.HTTP_400.getValue());
            msg.setMsg(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",参与者不能为空！");
            log.info(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",参与者不能为空！");
            return msg;
        }
        //判断开始时间是否为空
        if (videoStreamDeviceDetail.getStartTime() == null) {
            msg.setCode(ConstantInfo.HttpCode.HTTP_400.getValue());
            msg.setMsg(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",开始时间不能为空！");
            log.info(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",开始时间不能为空！");
            return msg;
        }
        //判断结束时间是否为空
        if (videoStreamDeviceDetail.getEndTime() == null) {
            msg.setCode(ConstantInfo.HttpCode.HTTP_400.getValue());
            msg.setMsg(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",结束时间不能为空！");
            log.info(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",结束时间不能为空！");
            return msg;
        }
        //判断通话时长是否为空
        if (videoStreamDeviceDetail.getSpendTime() == null || videoStreamDeviceDetail.getSpendTime() < 1) {
            msg.setCode(ConstantInfo.HttpCode.HTTP_400.getValue());
            msg.setMsg(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",通话时长不能为空！");
            log.info(ConstantInfo.HttpCode.HTTP_400.getDesc() + ",通话时长不能为空！");
            return msg;
        }
        //保存通话记录
        Msg resultMsg = videoStreamCompanyService.saveRecord(videoStreamDeviceDetail);
        msg.setSuccess(resultMsg.isSuccess());
        msg.setCode(resultMsg.isSuccess() ? ConstantInfo.HttpCode.HTTP_200.getValue() : ConstantInfo.HttpCode.HTTP_400.getValue());
        msg.setMsg(resultMsg.isSuccess() ? ConstantInfo.HttpCode.HTTP_200.getDesc() : resultMsg.getMsg());
        return msg;
    }
}
